package com.iflytek.uoamp.agent.monitor.find.imp;

import com.alibaba.fastjson.JSON;
import com.iflytek.uoamp.agent.common.util.DirectoryControl;
import com.iflytek.uoamp.agent.common.util.ReadFileByLine;
import com.iflytek.uoamp.agent.common.util.WFile;
import com.iflytek.uoamp.agent.core.sender.monitorsender.requestObj.FileName;
import com.iflytek.uoamp.agent.core.sender.monitorsender.requestObj.NewFileName;
import com.iflytek.uoamp.agent.core.sender.monitorsender.requestObj.PluginType;
import com.iflytek.uoamp.agent.core.sender.monitorsender.requestObj.SelectAll;
import com.iflytek.uoamp.agent.core.sender.monitorsender.responseObj.AutoPluginFile;
import com.iflytek.uoamp.agent.core.sender.monitorsender.responseObj.Program;
import com.iflytek.uoamp.agent.core.sender.monitorsender.responseObj.ProgramType;
import com.iflytek.uoamp.agent.monitor.find.CMDExextor;
import com.iflytek.uoamp.agent.monitor.find.CMDExextor.ShellResult;
import com.iflytek.uoamp.agent.monitor.find.Finder;
import com.iflytek.uoamp.agent.monitor.mcagent.CommonConstant;
import com.iflytek.uoamp.agent.monitor.mcagent.MCAgentConfig;
import com.iflytek.uoamp.agent.monitor.mcagent.MCAgentMain;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.*;

/**
 * @author gaowei shell 脚本组件的监控项发现;
 */
public class FindMonitorItemByShell implements Finder {

	private static String getValue(CMDExextor.ShellResult value, String defaultValue) {
		Logger.getLogger("debugFinder").info("value.output :" + value.output);
		String[] portList = value.output.split(CommonConstant.ComponentMCAgent.RESULTOUTPUT_SHELL_SPLIT);
		if (portList.length == 0) {
			return defaultValue;
		}
		return portList[0];
	}

	/**
	 * this function try get program's name,ip,port,path and flag,
	 * */
	private List<Program> getMonitorItem(String program) {
		String programName = null;
		String programType = null;
		try {
			programName = program.split("---")[0];
			programType = program.split("---")[1];
		} catch (Exception e) {
			Logger.getLogger("debugFinder").info("program info" + program);
			Logger.getLogger("debugFinder").info("erro:" + e);
			return null;
		}
		Logger.getLogger("debugFinder").info("check program " + programName);

		List<Program> list = new LinkedList<Program>();

		if (new File("cmd/linux/auto/" + programName + "/ps.sh").exists()) {
			ShellResult cmdResult = CMDExextor.execut("cmd/linux/auto/" + programName + "/ps.sh");
			Logger.getLogger("debugFinder").info("run cmd ps.sh result cmdResult.output content:" + cmdResult.output + "cmdResult.output is empty--->" + cmdResult.output == null || "".equals(cmdResult.output));
			if (cmdResult.output == null || "".equals(cmdResult.output))
				return null;
			String[] programList = cmdResult.output.split("\n");

			for (String cur : programList) {
				Program cprogram = new Program();
				cur = cur.replaceAll("\\s+", " ");

				String pid = null;
				String[] temp = cur.split(" ");
				if (temp.length < 2) {
					continue;
				}

				pid = temp[1];

				// guess path
				String path = "";
				boolean isExit = new File("cmd/linux/auto/" + programName + "/path.sh").exists();
				Logger.getLogger("debugFinder").info("path.s isExit is " + isExit);
				if (isExit) {
					Logger.getLogger("debugFinder").info("path.s run cmd/linux/auto/" + programName + "/path.sh " + pid);
					ShellResult pathResult = CMDExextor.execut("cmd/linux/auto/" + programName + "/path.sh " + pid);

					if (pathResult != null) {
						Logger.getLogger("debugFinder").info("path.s result is " + pathResult.output);
						path = getValue(pathResult, "");
					}
				}

				Logger.getLogger("debugFinder").info("ps info : " + cur);
				// programType:1为三方组件，2为业务组件
				if (programType.equals("1")) {
					cprogram.setType(ProgramType.COM);
				} else if (programType.equals("2")) {
					cprogram.setType(ProgramType.BIZ);
				}

				cprogram.setCpname(programName);
				cprogram.setIp(MCAgentConfig.getInstance().getMonitoredPointIp());

				// get port
				if (new File("cmd/linux/auto/" + programName + "/port.sh").exists()) {
					Logger.getLogger("debugFinder").info("run cmd/linux/auto/" + programName + "/port.sh " + pid);
					ShellResult curResult = CMDExextor.execut("cmd/linux/auto/" + programName + "/port.sh " + pid);
					String value = getValue(curResult, "0");
					Logger.getLogger("debugFinder").info("run port and get result is " + curResult.output);
					if (value != null && !value.equals("")) {
						if (StringUtils.isNumeric(value)) {
							int port = Integer.parseInt(value);
							cprogram.setPort(port);
							Logger.getLogger("debugFinder").info("parse port get port is number!!! " + port);
						} else {
							try {
								Logger.getLogger("debugFinder").info("parse port by ip ");
								value = value.replaceAll("\\s+", " ");
								String portStr = value.split(" ")[3];
								Logger.getLogger("debugFinder").info("parse port by get port str is  " + portStr);
								String[] portNum = portStr.split(":");
								if (portNum.length == 0) {
									continue;
								}
								int port = Integer.parseInt(portNum[portNum.length - 1].trim());
								cprogram.setPort(port);
							} catch (Exception e) {
								Logger.getLogger("debugFinder").error(e.getMessage(), e);
								continue;
							}
						}
					}
				}
				if (path != null) {
					cprogram.setPath(path);
				}

				isExit = new File("cmd/linux/auto/" + programName + "/flag.sh").exists();
				Logger.getLogger("debugFinder").info("flag.s isExit is " + isExit);
				if (isExit) {
					Logger.getLogger("debugFinder").info("flag.s run cmd/linux/auto/" + programName + "/flag.sh " + cur);
					ShellResult flag = CMDExextor.execut("cmd/linux/auto/" + programName + "/flag.sh " + cur);
					Logger.getLogger("debugFinder").info("flag.s result is " + flag.output);
					cprogram.setFlags(Arrays.asList(flag.output.split("\n")));
				} else {
					cprogram.setFlags(Arrays.asList(pid));
				}
				// getMAC
				cprogram.setMac(MCAgentConfig.getInstance().getMonitoredPointMAC());
				// get机房名称
				cprogram.setHostroom(MCAgentConfig.getInstance().getEngineRoomName());
				// get主机名称
				cprogram.setHostname(MCAgentConfig.getInstance().getMonitoredHostName());
				if (cprogram.getPort() != 0 && cprogram.getPort() <= 65535) {
					list.add(cprogram);
				}

			}
		}

		return list;
	}

	private List<Program> runAutoWidget(String program) {
		try {
			Logger.getLogger("debugFinder").info("check program " + program);
			ShellResult cmdResult = CMDExextor.execut("upload/auto/" + program + "/ps.sh");
			Logger.getLogger("debugFinder").info("run cmd ps.sh result cmdResult.output content:" + cmdResult.output + "cmdResult.output is empty--->" + cmdResult.output == null || "".equals(cmdResult.output));
			if (cmdResult.output == null || "".equals(cmdResult.output))
				return null;
			String[] programList = cmdResult.output.split("\n");
			List<Program> list = new LinkedList<Program>();

			// guess path
			String path = "";
			boolean isExit = new File("upload/auto/" + program + "/path.sh").exists();
			Logger.getLogger("debugFinder").info("path.s isExit is " + isExit);
			if (isExit) {
				Logger.getLogger("debugFinder").info("path.s run upload/auto/" + program + "/path.sh " + program);
				ShellResult pathResult = CMDExextor.execut("upload/auto/" + program + "/path.sh " + program);

				if (pathResult != null) {
					Logger.getLogger("debugFinder").info("path.s result is " + pathResult.output);
					path = getValue(pathResult, "");
				}
			}

			for (String cur : programList) {

				Program cprogram = new Program();
				cur = cur.replaceAll("\\s+", " ");

				Logger.getLogger("debugFinder").info("ps info : " + cur);

				String pid = null;
				String[] temp = cur.split(" ");
				if (temp.length < 2) {
					continue;
				}

				pid = temp[1];
				// if(program.indexOf(".")!=-1){//包含.就是业务组件
				// cprogram.setType(ProgramType.LIB);
				// }else{
				cprogram.setType(ProgramType.COM);
				// }
				cprogram.setCpname(program);
				cprogram.setIp(MCAgentConfig.getInstance().getMonitoredPointIp());

				// get port
				if (new File("upload/auto/" + program + "/port.sh").exists()) {
					Logger.getLogger("debugFinder").info("run upload/auto/" + program + "/port.sh " + pid);
					ShellResult curResult = CMDExextor.execut("upload/auto/" + program + "/port.sh " + pid);
					String value = getValue(curResult, "0");
					Logger.getLogger("debugFinder").info("run port and get result is " + curResult.output);
					if (value != null && !value.equals("")) {
						if (StringUtils.isNumeric(value)) {
							Logger.getLogger("debugFinder").info("parse port get port is number!!! ");
							int port = Integer.parseInt(value);
							cprogram.setPort(port);
						} else {
							try {
								Logger.getLogger("debugFinder").info("parse port by ip ");
								value = value.replaceAll("\\s+", " ");
								String portStr = value.split(" ")[3];
								Logger.getLogger("debugFinder").info("parse port by get port str is  " + portStr);
								String[] portNum = portStr.split(":");
								if (portNum.length == 0) {
									continue;
								}
								int port = Integer.parseInt(portNum[portNum.length - 1].trim());
								cprogram.setPort(port);
							} catch (Exception e) {
								Logger.getLogger("debugFinder").error(e.getMessage(), e);
								continue;
							}
						}
					}
				}

				if (path != null) {
					cprogram.setPath(path);
				}

				// get flags
				isExit = new File("upload/auto/" + program + "/flag.sh").exists();
				Logger.getLogger("debugFinder").info("flag.s isExit is " + isExit);
				if (isExit) {
					Logger.getLogger("debugFinder").info("flag.s run upload/auto/" + program + "/flag.sh " + cur);
					ShellResult flag = CMDExextor.execut("upload/auto/" + program + "/flag.sh " + cur);
					Logger.getLogger("debugFinder").info("flag.s result is " + flag.output);
					cprogram.setFlags(Arrays.asList(flag.output.split("\n")));
				} else {
					cprogram.setFlags(Arrays.asList(pid));
				}

				// getMAC
				cprogram.setMac(MCAgentConfig.getInstance().getMonitoredPointMAC());
				// get机房名称
				cprogram.setHostroom(MCAgentConfig.getInstance().getEngineRoomName());
				// get主机名称
				cprogram.setHostname(MCAgentConfig.getInstance().getMonitoredHostName());
				Logger.getLogger("debugFinder").info("program isAdd : " + (cprogram.getPort() <= 65535));

				if (/* cprogram.getPort()!=0 && */cprogram.getPort() <= 65535) {
					list.add(cprogram);
				}
				Logger.getLogger("debugFinder").info(cprogram.toString());
			}
			return list;
		} catch (Exception e) {
			Logger.getLogger("debugFinder").error("run runAutoWidget fail!" , e);
		}
		return null;
	}

	public List<Program> getMonitorItemByServer() {
		List<Program> listResult = new LinkedList<Program>();
		File file = new File("");
		String dir = file.getAbsolutePath();
		FileName fn = new FileName();
		// 自动发现请求是列表
		fn.setFileName("");
		// 1--自动发现插件,2--监控插件
		fn.setFlag("1");
		Object resultTemp = MCAgentMain.getSenderClient().sendBySafe(fn);
		if (resultTemp != null && resultTemp instanceof List) {
			@SuppressWarnings("unchecked")
			List<AutoPluginFile> tempList = (List<AutoPluginFile>) resultTemp;
			for (int i = 0; i < tempList.size(); i++) {
				// 写入本地
				if (WFile.wfByByte(dir + "/upload/zip/" + tempList.get(i).getFilename(), tempList.get(i).getFilebyte())) {
					Logger.getRootLogger().info("upload file " + tempList.get(i).getFilename() + " success");
					// 解压
					WFile.unZipFiles(dir + "/upload/zip/" + tempList.get(i).getFilename(), dir + "/upload/auto/");
					/*
					 * //赋予权限 try {
					 * Runtime.getRuntime().exec("chmod 700 -R upload/auto"); }
					 * catch (IOException e) { e.printStackTrace(); }
					 */
					// 执行自动发现插件
					Logger.getRootLogger().info("runAutoWidget program :" + tempList.get(i).getFilename().split(".zip")[0]);
					try {
						List<Program> list = runAutoWidget(tempList.get(i).getFilename().split(".zip")[0]);
						listResult.add(list.get(0));
					} catch (Exception e) {
						Logger.getRootLogger().error("runAutoWidget program" + tempList.get(i).getFilename() + " exception!");
					}
					// 解压完之后删掉压缩包
					File fileZip = new File(dir + "/upload/zip/" + tempList.get(i).getFilename());
					fileZip.delete();
				} else {
					Logger.getRootLogger().info("upload file " + tempList.get(i).getFilename() + " faild");
				}
			}
		}
		Logger.getRootLogger().info("sendProgram serverResult number :  " + listResult.size());
		return listResult;
	}

	/**
	 * find all programs
	 * */
	public List<Program> getMonitorItems() {

		/*
		 * ==================================开始更新自发现插件============================
		 * ======
		 */

		Logger.getLogger("debugFinder").info("start find programs \n\n\n");
		// 从mc-server得到所有插件列表并写入cmd/linux/auto/programs.list
		SelectAll fn = new SelectAll();
		Object allPluginMap = MCAgentMain.getSenderClient().sendBySafe(fn);
//		String s = "";
		StringBuffer buf = new StringBuffer();
		@SuppressWarnings("unchecked")
		Map<String, String> pluginListMap = (Map<String, String>) allPluginMap;
		Logger.getLogger("debugFinder").info("三方组件列表:=====================>" + JSON.toJSONString(pluginListMap));
		if (pluginListMap != null && pluginListMap.size() >= 1) {
			for (Map.Entry<String,String> entry : pluginListMap.entrySet()) {
//				s = s + item + "---" + pluginListMap.get(item).split((char) 30 + "")[0] + "\r\n";
				buf.append(entry.getKey() + "---" + entry.getValue().split((char) 30 + "")[0] + "\r\n");
			}
//			WFile.wf("cmd/linux/auto/programs.list", s);
			WFile.wf("cmd/linux/auto/programs.list", buf.toString());
		}
		// 调获取自发现插件文件列表含更新时间
		PluginType pt = new PluginType();
		pt.setType("1");
		Object autoPluginMap = MCAgentMain.getSenderClient().sendBySafe(pt);
		@SuppressWarnings("unchecked")
		Map<String, String> autoPluginListMap = (Map<String, String>) autoPluginMap;
		Logger.getLogger("debugFinder").info("自发现插件列表:=====================>" + JSON.toJSONString(autoPluginListMap));
		if (autoPluginListMap != null && autoPluginListMap.size() >= 1) {
			// 判断autoPluginUpadte.list文件是否存在
			String commd = "cmd/linux/auto/autoPluginUpadte.list";
			File flieCmd = new File(commd);
			// 如果不存在，则下载全部的自发现组件并写入autoPluginUpadte.list
			if (!flieCmd.exists()) {
//				String content = "";
				StringBuffer content = new StringBuffer();
				for (Map.Entry<String,String> entry : autoPluginListMap.entrySet()) {
					NewFileName nfn = new NewFileName();
					nfn.setFileName(entry.getKey());
					getMonitorItemByServer(nfn);
//					content = content + key + "==========" + autoPluginListMap.get(key) + "\r\n";
					content.append(entry.getKey() + "==========" + entry.getValue() + "\r\n");
				}
//				WFile.wf(commd, content);
				WFile.wf(commd, content.toString());
			}

			// 获取autoPluginUpadte.list的键值存放到autoPluginUpadteMap
			Map<String, String> autoPluginUpadteMap = new HashMap<String, String>();
			List<String> autoPluginUpadtelines = ReadFileByLine.getAllLine2Array("cmd/linux/auto/autoPluginUpadte.list");
			String[] arges;
			for (String cur : autoPluginUpadtelines) {
				arges = cur.split("==========");
				autoPluginUpadteMap.put(arges[0], arges[1]);
			}

			// map里面若包含 判断时间与本地是否一致 一致直接调用本地脚本 不一致调用文件下载接口下载文件
			// 若map不包含 调用本地 本地无不执行
			for (Map.Entry<String,String> entry : autoPluginListMap.entrySet()) {
				if (pluginListMap.containsKey(entry.getKey().split(".zip")[0])) {
					if (!entry.getValue().equals(autoPluginUpadteMap.get(entry.getKey()))) {
						NewFileName nfn = new NewFileName();
						nfn.setFileName(entry.getKey());
						getMonitorItemByServer(nfn);
						// 下载之后更新组件的更新时间
						autoPluginUpadteMap.put(entry.getKey(), entry.getValue());

					}
				}
			}
			// 对比完成之后再次写入autoPluginUpadte.list
//			String content = "";
			StringBuffer content = new StringBuffer();
			for (Map.Entry<String,String> entry : autoPluginListMap.entrySet()) {
//				content = content + key + "==========" + autoPluginListMap.get(key) + "\r\n";
				content.append(entry.getKey() + "==========" + entry.getValue() + "\r\n");
			}
//			WFile.wf(commd, content);
			WFile.wf(commd, content.toString());
		}

		/*
		 * ==================================开始更新监控插件============================
		 * ======
		 */

		// 调获取监控插件文件列表含更新时间
		pt.setType("2");
		Object monitorPluginMap = MCAgentMain.getSenderClient().sendBySafe(pt);
		@SuppressWarnings("unchecked")
		Map<String, String> monitorPluginListMap = (Map<String, String>) monitorPluginMap;
		Logger.getLogger("debugFinder").info("监控插件列表:=====================>" + JSON.toJSONString(monitorPluginListMap));
		if (monitorPluginListMap != null && monitorPluginListMap.size() >= 1) {
			// 判断monitor.list文件是否存在
			String monitorcommd = "cmd/linux/monitorPluginUpadte.list";
			File monitorflieCmd = new File(monitorcommd);
			// 如果不存在，则下载全部的监控插件并写入autoPluginUpadte.list
			if (!monitorflieCmd.exists()) {
//				String contents = "";
				StringBuffer contents = new StringBuffer();
				for (Map.Entry<String,String> entry : monitorPluginListMap.entrySet()) {
					NewFileName nfn = new NewFileName();
					nfn.setFileName(entry.getKey());
					checkMonitor(nfn);
//					contents = contents + key + "========" + monitorPluginListMap.get(key) + "\r\n";
					contents.append(entry.getKey() + "========" + entry.getValue() + "\r\n");
				}
//				WFile.wf(monitorcommd, contents);
				WFile.wf(monitorcommd, contents.toString());
			}

			// 获取autoPluginUpadte.list的键值存放到autoPluginUpadteMap
			Map<String, String> monitorPluginUpadteMap = new HashMap<String, String>();
			List<String> monitorPluginUpadtelines = ReadFileByLine.getAllLine2Array("cmd/linux/monitorPluginUpadte.list");
			String[] monitorarges;
			for (String cur : monitorPluginUpadtelines) {
				monitorarges = cur.split("========");
				monitorPluginUpadteMap.put(monitorarges[0], monitorarges[1]);
			}

			// 判断时间与本地是否一致 一致直接调用本地脚本 不一致调用文件下载接口下载文件
			for (Map.Entry<String,String> entry : monitorPluginListMap.entrySet()) {
				if (!entry.getValue().equals(monitorPluginUpadteMap.get(entry.getKey()))) {
					NewFileName nfn = new NewFileName();
					nfn.setFileName(entry.getKey());
					checkMonitor(nfn);
					// 下载之后更新组件的更新时间
					monitorPluginUpadteMap.put(entry.getKey(), entry.getValue());
				}

			}

			// 对比完成之后再次写入autoPluginUpadte.list
//			String contents = "";
			StringBuffer contents = new StringBuffer();
			for (Map.Entry<String,String> entry : monitorPluginListMap.entrySet()) {
//				contents = contents + key + "========" + monitorPluginListMap.get(key) + "\r\n";
				contents.append(entry.getKey() + "========" + entry.getValue() + "\r\n");
			}
//			WFile.wf(monitorcommd, contents);
			WFile.wf(monitorcommd, contents.toString());
		}

		List<Program> result = new LinkedList<Program>();
		if (new File("cmd/linux/auto/programs.list").exists()) {
			List<String> lines = ReadFileByLine.getAllLine2Array("cmd/linux/auto/programs.list");
			if (lines != null && lines.size() >= 1) {
				for (String cur : lines) {
					List<Program> tempResult = getMonitorItem(cur);
					if (tempResult != null) {
						result.addAll(tempResult);
					}
				}
			}
		}
		Logger.getLogger("debugFinder").info("自发现结果==============》" + JSON.toJSONString(result));
		Logger.getLogger("debugFinder").info("finish find programs \n\n\n");
		return result;
	}

	/**
	 * this function is single finder ,just for test
	 * */
	public List<Program> getMonitorItemForSingle(String program) {
		List<Program> programs = getMonitorItem(program);
		return programs;
	}

	public void getMonitorItemByServer(NewFileName nfn) {
		File file = new File("");
		String dir = file.getAbsolutePath();
		AutoPluginFile resultTemp = (AutoPluginFile) MCAgentMain.getSenderClient().sendBySafe(nfn);
		if (resultTemp != null) {
			String filenameCpl = resultTemp.getFilename();
			String filename = filenameCpl.split(".zip")[0];
			// 写入本地
			if (WFile.wfByByte(dir + "/upload/zip/" + filenameCpl, resultTemp.getFilebyte())) {
				Logger.getRootLogger().info("upload file " + filenameCpl + " success");
				// 删除源目录下旧文件
				File oldFile = new File(dir + "/cmd/linux/auto/" + filename);
				if (oldFile.exists()) {
					if (oldFile.isDirectory()) {
						DirectoryControl.delAllFile(dir + "/cmd/linux/auto/" + filename);
					} else if (oldFile.isFile()) {
						oldFile.delete();
					}
				}
				// 解压
				WFile.unZipFiles(dir + "/upload/zip/" + filenameCpl, dir + "/cmd/linux/auto/");
				// 解压完之后删掉压缩包
				File fileZip = new File(dir + "/upload/zip/" + resultTemp.getFilename());
				fileZip.delete();
			} else {
				Logger.getRootLogger().info("upload file " + filenameCpl + " faild");
			}

		}
	}

	public void checkMonitor(NewFileName nfn) {
		try {
			File file = new File("");
			String dir = file.getAbsolutePath();
			AutoPluginFile resultTemp = (AutoPluginFile) MCAgentMain.getSenderClient().sendBySafe(nfn);
			if (resultTemp != null) {
				String filenameCpl = resultTemp.getFilename();
				String filename;
				Logger.getRootLogger().info("filename: " + filenameCpl);
				if (filenameCpl.indexOf(".zip") != -1) {// 如果是ZIP包
					filename = filenameCpl.split(".zip")[0];
					copyFileToDestinationFile(filename, dir, filenameCpl, resultTemp);
				} else if (filenameCpl.indexOf(".sh") != -1) {// 如果是.sh文件
					filename = filenameCpl.split(".sh")[0];
					copyFileToDestinationFile(filename, dir, filenameCpl, resultTemp);
				} else if (filenameCpl.indexOf(".py") != -1) {// 如果是.py文件
					filename = filenameCpl.split(".py")[0];
					copyFileToDestinationFile(filename, dir, filenameCpl, resultTemp);
				} else if (filenameCpl.indexOf(".jar") != -1) {// 如果是.jar文件
					filename = filenameCpl.split(".jar")[0];
					copyFileToDestinationFile(filename, dir, filenameCpl, resultTemp);
				} else {
					Logger.getRootLogger().info("upload file " + filenameCpl + " faild");
				}

				Properties props = System.getProperties();
				String osName = props.getProperty("os.name");

				if (osName.toLowerCase().contains("linux") || osName.toLowerCase().contains("mac")) {
					// 赋予权限
					Runtime.getRuntime().exec("chmod 700 -R " + "cmd");
				}
			}
		} catch (Exception e) {
			Logger.getRootLogger().info("checkMonitor faild!" + e.toString());
		}
	}

	public void copyFileToDestinationFile(String filename, String dirName, String fullfilename, AutoPluginFile pluginFile) {
		// 删除源目录下旧文件
		if (fullfilename.indexOf(".zip") != -1) {
			DirectoryControl.delAllFile(dirName + "/cmd/linux/" + filename);
			DirectoryControl.delAllFile(dirName + "/cmd/windows/" + filename);

			// 写入本地
			if (WFile.wfByByte(dirName + "/upload/zip/" + fullfilename, pluginFile.getFilebyte())) {
				Logger.getRootLogger().info("upload file " + fullfilename + " success");
				// 解压
				WFile.unZipFiles(dirName + "/upload/zip/" + fullfilename, dirName + "/cmd/linux/");
				WFile.unZipFiles(dirName + "/upload/zip/" + fullfilename, dirName + "/cmd/windows/");

				// 解压完之后删掉压缩包
				File fileZip = new File(dirName + "/upload/zip/" + fullfilename);
				fileZip.delete();
			} else {
				Logger.getRootLogger().info("upload file " + fullfilename + " faild");
			}
		} else {
			File oldLinuxFile = new File(dirName + "/cmd/linux/" + fullfilename);
			if (oldLinuxFile.exists()) {
				oldLinuxFile.delete();
			}

			File oldWinFile = new File(dirName + "/cmd/windows/" + fullfilename);
			if (oldWinFile.exists()) {
				oldWinFile.delete();
			}

			// 写入本地
			if (!WFile.wfByByte(dirName + "/cmd/linux/" + fullfilename, pluginFile.getFilebyte())) {
				Logger.getRootLogger().info("upload file " + fullfilename + " faild");
			}

			// 写入本地
			if (!WFile.wfByByte(dirName + "/cmd/windows/" + fullfilename, pluginFile.getFilebyte())) {
				Logger.getRootLogger().info("upload file " + fullfilename + " faild");
			}
		}
	}

}